package com.example.springsecurity.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springsecurity.entity.Menu;
import com.example.springsecurity.entity.Role;
import com.example.springsecurity.entity.Users;
import com.example.springsecurity.mapper.UserInfoMapper;
import com.example.springsecurity.mapper.UserMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: UserService
 * Author:   WGG
 * Date:    2022-12-2022/12/19-20:41
 * Version: 1.0
 * Description:
 */
@Service("userDetailsService")
public class UserService implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        //加设数据库中的账户   wgg 123456
//        //使用username跟admin进行比较
//        if (!"wgg".equals(username)){
//            throw new UsernameNotFoundException("用户名不存在");
//        }
//        String pwd = "$2a$10$xFluBUUTSV0CsjjrvcAfxuETHu8NobX6d8LW5K9iM3Bn4ju31wsSe";
//        return new User(username,pwd, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,role"));
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Users user = userMapper.selectOne(wrapper);
        if (user==null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        // 获取角色
        List<Role> roles = userInfoMapper.selectRoleUserId(user.getId().longValue());
        // 获取权限
        List<Menu> menus = userInfoMapper.selectMenuByUserId(user.getId().longValue());
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        // 授予角色
        for (Role role : roles) {
            SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_" + role.getName());
            grantedAuthorities.add(grantedAuthority);
        }
        // 授予权限
        for (Menu menu : menus) {
            SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(menu.getPermission());
            grantedAuthorities.add(grantedAuthority);
        }
        //设置权限和角色
//        List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
//        List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_role,ROLE_admin");
        return new User(username,user.getPassword(),grantedAuthorities);
    }
}
